package com.hilaver.dzmis.exhibition.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.exhibition.hibernate.TechnicChart;
import com.hilaver.dzmis.exhibition.hibernate.TechnicChartMaterial;
import com.hilaver.dzmis.exhibition.hibernate.Yarn;
import com.hilaver.dzmis.exhibition.service.TechnicChartService;
import com.hilaver.dzmis.exhibition.utils.Constants;
import com.hilaver.dzmis.exhibition.utils.StringUtils;

/**
 * Servlet implementation class for Servlet: TechnicChartServlet
 * 
 */
public class TechnicChartServlet extends
		com.hilaver.dzmis.exhibition.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	public final static int ACTION_SAVE_MATERIAL = 200;
	
	public final static int ACTION_GET_ALL_MATERIAL = 201;
	
	public final static int ACTION_DELETE_MATERIAL = 202;
	
	public final static int ACTION_GET_MATERIAL = 203;
	
	private TechnicChartMaterial tcm;

	public TechnicChartServlet() {
		this.fullName = TechnicChart.class.getName();
		this.simpleName = TechnicChart.class.getSimpleName();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);

		TechnicChartService serviceObj = new TechnicChartService();
		Method method = null;
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";
			this.setPaginationParam(request);
			method = serviceObj.getClass().getMethod("getAllPagination",
					new Class[] { int.class, String.class, String.class });// int
			// page,
			// String
			// sort,
			// String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir });
			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			method = serviceObj.getClass().getMethod("edit",
					new Class[] { Object.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.obj });
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			method = serviceObj.getClass().getMethod("delete",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			method = serviceObj.getClass().getMethod("get",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setPaginationParam(request);
			this.setFilterParam(request);
			method = serviceObj.getClass().getMethod(
					"getAllPagination",
					new Class[] { int.class, String.class, String.class,
							String[].class });// int page, String sort, String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir, this.filters });
			break;
		case ACTION_GET_ALL:
			this.setPaginationParam(request);
			method = serviceObj.getClass().getMethod("getAll",
					new Class[] { String.class, String.class });// String sort,
			// String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.sort,
					this.dir });
			break;
		case ACTION_SAVE_MATERIAL:
			this.setId(request);
			this.fillMaterial(request);
			rtn = serviceObj.editTechinicChartMaterial(this.tcm);
			break;
		case ACTION_GET_ALL_MATERIAL:
			this.setId(request);
			rtn = serviceObj.getAllTechnicChartMaterial(this.id);
			break;
		case ACTION_DELETE_MATERIAL:
			this.setId(request);
			rtn = serviceObj.deleteTechnicChartMaterial(this.id);
			break;
		case ACTION_GET_MATERIAL:
			this.setId(request);
			rtn = serviceObj.getTechnicChartMaterial(this.id);
			break;
		}

		return rtn;
	}
	
	private void fillMaterial(HttpServletRequest request){
		this.tcm = new TechnicChartMaterial();
		TechnicChart tc = new TechnicChart();
		tc.setId(Integer.parseInt(request.getParameter("tcId")));
		Yarn yarn = new Yarn();
		yarn.setId(Integer.parseInt(request.getParameter("yarnId")));
		
		this.tcm.setId(this.id);
		this.tcm.setTechnicChart(tc);
		this.tcm.setYarn(yarn);
		this.tcm.setUnitPrice(Double.parseDouble(request.getParameter("unitPrice")));
		if(!StringUtils.isEmpty(request.getParameter("q1"))){
			this.tcm.setQ1(Double.parseDouble(request.getParameter("q1")));
			this.tcm.setC1(Double.parseDouble(request.getParameter("c1")));
		}
		if(!StringUtils.isEmpty(request.getParameter("q2"))){
			this.tcm.setQ2(Double.parseDouble(request.getParameter("q2")));
			this.tcm.setC2(Double.parseDouble(request.getParameter("c2")));
		}
		if(!StringUtils.isEmpty(request.getParameter("q3"))){
			this.tcm.setQ3(Double.parseDouble(request.getParameter("q3")));
			this.tcm.setC3(Double.parseDouble(request.getParameter("c3")));
		}
		if(!StringUtils.isEmpty(request.getParameter("q4"))){
			this.tcm.setQ4(Double.parseDouble(request.getParameter("q4")));
			this.tcm.setC4(Double.parseDouble(request.getParameter("c4")));
		}
		if(!StringUtils.isEmpty(request.getParameter("q5"))){
			this.tcm.setQ5(Double.parseDouble(request.getParameter("q5")));
			this.tcm.setC5(Double.parseDouble(request.getParameter("c5")));
		}
		if(!StringUtils.isEmpty(request.getParameter("otherCo"))){
			this.tcm.setOtherCo(Double.parseDouble(request.getParameter("otherCo")));
			this.tcm.setOtherCo(Double.parseDouble(request.getParameter("otherCo")));
		}
		
	}
}